var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { setPosition } from "../action/newTransfrom.js";
import { options, state } from "../config.js";
import { box, currentState, domg } from "../index.js";
import { emit, on } from "../utils/scoket.js";
import { closeMask, openMask, setMaskContent } from "./mask.js";
var app = document.querySelector("#app");
//弹幕功能
var count = 0;
export var barrageFun = function (_a) {
    var key = _a.key;
    if (key !== "t")
        return;
    count++;
    if (count >= 20) {
        openMask();
        openBarrage(app);
        count = 0;
    }
};
//弹幕面板的模板
var template = "    \n<div class=\"barrageBox\">\n  <div class=\"row\">\n    <input class=\"barrageIpt\" />\n  </div>\n  <div class=\"row\">\n    <button class=\"barrageBiu\">\u53D1\u5C04</button>\n  </div>\n</div>";
var openBarrage = function (app) {
    setMaskContent(template);
    //获取焦点以及回车发送
    setTimeout(function () {
        var ipt = domg(".barrageIpt");
        ipt.focus();
        ipt.onkeydown = function (_a) {
            var key = _a.key;
            if (key === "Enter")
                return domg(".barrageBiu").click();
        };
    }, 500);
    domg(".barrageBiu").onclick = function () {
        var ipt = domg(".barrageIpt"); //拿到ipt内容
        closeMask();
        createBarrage(ipt.value, app, options);
    };
};
on.all("/barrage", function (_a) {
    var value = _a.value;
    if (value.role !== currentState.role) {
        createBarrage(value.str, app, value);
    }
});
/**
 *
 * @param str 创建弹幕的内容
 * @param ele 在那个地方创建弹幕
 */
var createBarrage = function (str, ele, options) {
    //根据弹幕类型生成弹幕
    options.role ||
        emit.all(currentState.role, "/barrage", __assign({ role: currentState.role, str: str }, options)); //发送请求
    switch (options.type) {
        //滚动弹幕
        case "rolling":
            rollingBarrage(str, ele, options);
            break;
    }
};
function rollingBarrage(str, ele, options) {
    var head = options.role || state.roleName;
    //创建弹幕实体
    var barrage = document.createElement("div");
    barrage.classList.add("barrage");
    barrage.style.display = "inline-block";
    barrage.style.color = options.barrageColor; //弹幕字体颜色
    barrage.style.backgroundColor = options.bgc;
    barrage.innerText = head + " : " + str;
    //弹幕生成位置
    var po = {
        x: box.boxW - 100,
        y: Math.floor(Math.random() * (box.boxH + 60) - 26)
    };
    setPosition(barrage, po);
    ele.append(barrage);
    barrage.getBoundingClientRect;
    var barrageW = barrage.offsetWidth;
    //创建弹幕
    var timer;
    timer = setInterval(function () {
        if (po.x <= -barrageW)
            return clearInterval(timer);
        po.x -= options.barrageSpeed;
        setPosition(barrage, po);
    }, 100);
}
